﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class _Default : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                FillData();
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            var dt = new DataTable();
            dt.Columns.Add(new DataColumn("Id"));
            dt.Columns.Add(new DataColumn("ProductName"));
            dt.Columns.Add(new DataColumn("ShortDesc"));
            dt.Columns.Add(new DataColumn("Remarks"));

            foreach(GridViewRow row in gv3.Rows)
            {
                var tempRow = dt.NewRow();
                tempRow["Id"] = GetValue(row.RowIndex, 0);
                tempRow["ProductName"] = GetValue(row.RowIndex, 1).Replace("&nbsp;","");
                tempRow["ShortDesc"] = GetValue(row.RowIndex, 2).Replace("&nbsp;", "");
                tempRow["Remarks"] = GetValue(row.RowIndex, 3).Replace("&nbsp;", "");

                dt.Rows.Add(tempRow);
            }

            dt.Rows.Add(dt.NewRow());

            gv3.DataSource = dt;
            gv3.DataBind();

            gv3.EditIndex = dt.Rows.Count - 1;

            gv3.DataSource = dt;
            gv3.DataBind();
        }

        protected void gv3_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gv3.EditIndex = -1;
            FillData();
        }

        private void FillData()
        {
            var sql = "select * from products";
            var dt = DbHelper.GetDataTable(sql);
            gv3.DataSource = dt;
            gv3.DataBind();
        }

        protected void gv3_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            var id = GetValue(e.RowIndex, 0);
            var sql = string.Format("delete from products where id={0}",  id);
            DbHelper.Exe(sql);

            gv3.EditIndex = -1;
            FillData();
        }

        protected void gv3_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gv3.EditIndex = e.NewEditIndex;
            FillData();
        }

        protected void gv3_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            var idString = GetValue(e.RowIndex, 0);
            var isOk = int.TryParse(idString, out int id);
            var pname = GetValue(e.RowIndex, 1);
            var desc = GetValue(e.RowIndex, 2);
            var remarks = GetValue(e.RowIndex, 3);

            if (isOk)
            {
                //表示id存在，是为更新
                var sql = string.Format("update products set productname='{0}',shortdesc='{1}',remarks='{2}' where id={3}", pname, desc, remarks, id);
                DbHelper.Exe(sql);
            }
            else
            {
                //表示id不存在，是为添加
                var sql = string.Format("insert into Products (ProductName,ShortDesc,Remarks) values ('{0}','{1}','{2}')",pname,desc,remarks);
                DbHelper.Exe(sql);
            }


            

            gv3.EditIndex = -1;
            FillData();
        }

        private string GetValue(int rowIndex, int colIndex)
        {
            var c = gv3.Rows[rowIndex].Cells[colIndex];
            var value = c.Controls.Count > 0 ? ((TextBox)c.Controls[0]).Text : c.Text;
            return value;
        }
    }
}